﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockCommon.baseclass;
using Utilities;

namespace StockCommon.TaskLog
{
    public class ObjectExecServices:DBAccess
    {
        public static bool islogComplete(ObjectExecLog exelog)
        {
            if (LogStatus.sUpdated.Equals((LogStatus)exelog.status))
            {
                if (LogResult.rComplete.Equals((LogResult)exelog.result))
                {
                    return true;
                }
            }
            return false;
        }

        public static Guid getServicesId(String type, String taskName, String version, bool create = true)
        {
            StockDataClassesDataContext context = getContext();

            ObjectId oid = (from oi in context.ObjectIds where (oi.objectType == type && oi.objectName == taskName && oi.version == version) select oi).FirstOrDefault<ObjectId>();
            if (oid != null)
                return oid.id;
            if (create == false)
                return Guid.Empty;

            oid = new ObjectId();
            oid.id = NewGuid.create("fath");
            oid.objectType = type;
            oid.objectName = taskName;
            oid.version = version;
            oid.actived = true;
            context.ObjectIds.InsertOnSubmit(oid);
            context.SubmitChanges();
            return oid.id;
        }
        public static ObjectExecLog getExecLog(Guid id, String action, DateTime date)
        {
            date = DateUtils.getDateOnly(date);
            StockDataClassesDataContext context = getContext();
            ObjectExecLog log = (from o in context.ObjectExecLogs where (o.id == id && o.action == action && o.dataDate == date) select o).FirstOrDefault<ObjectExecLog>();
            if (log != null)
                return log;
            log = new ObjectExecLog();
            log.id = id;
            log.action = action;
            log.dataDate = date;
            log.msg = "";
            log.status = (int)LogStatus.sUnProcessed;
            log.result = (int)LogResult.rNoData;

            context.ExecuteCommand("INSERT INTO  [ObjectExecLog]  ([id],[dataDate] ,[action],[status],[result],[msg])  VALUES  ({0},{1},{2},{3},{4},{5})",
                                                               log.id, log.dataDate, log.action, log.status, log.result, log.msg);
            context.SubmitChanges();
            //context.ObjectExecLogs.InsertOnSubmit(log);
            //context.SubmitChanges();
            return log;
        }
        public static void updateExecLog(ObjectExecLog log)
        {
            StockDataClassesDataContext context = getContext();

            context.ExecuteCommand(" UPDATE [ObjectExecLog] set  status ={0} , result={1}, msg={2}  where id={3} and action={4} and dataDate={5} ",
                new object[] { log.status, log.result, log.msg, log.id, log.action, log.dataDate });
            context.SubmitChanges();
        }
    }

    public enum LogStatus : int
    {
        sUnProcessed = 0,
        sUpdated = 1,
        sConnectFailed = 2,
        sPageNotFound=3,
    }
    public enum LogResult : int
    {
        rNoData = 10,
        rComplete = 11,
        rDataError = 12,
    }
}
